package com.midea.ai.air.view;

import android.app.Dialog;
import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.midea.ai.air.R;

/**
 * Created by ouyangdi on 2016/4/11.
 * 中间弹出的对话框View
 */
public class AlertCenterDialog {
    private Context mContext;
    private Dialog mDialog;
    private TextView mTvTitle;
    private TextView mTvMsg;
    private Button mBtnNeg;
    private Button mBtnPos;
    private View mDivider;

    private Boolean mIsShowTitle = false;
    private Boolean mIsShowMsg = false;
    private Boolean mIsShowNegBtn = false;
    private Boolean mIsShowPosBtn = false;

    public AlertCenterDialog(Context context) {
        mContext = context;
    }

    public AlertCenterDialog builder() {
        //获取Dialog布局
        View view = View.inflate(mContext, R.layout.dialog_item, null);

        //获取自定义Dialog布局中的控件
        mTvTitle = (TextView) view.findViewById(R.id.dialog_item_textView_title);
        mTvTitle.setVisibility(View.GONE);
        mTvMsg = (TextView) view.findViewById(R.id.dialog_item_textView_msg);
        mTvMsg.setVisibility(View.GONE);
        mBtnNeg = (Button) view.findViewById(R.id.dialog_item_textView_cancel);
        mBtnNeg.setVisibility(View.GONE);
        mBtnPos = (Button) view.findViewById(R.id.dialog_item_textView_confirm);
        mBtnPos.setVisibility(View.GONE);
        mDivider = view.findViewById(R.id.dialog_line);
        mDivider.setVisibility(View.GONE);

        //定义Dialog布局和参数
        mDialog = new Dialog(mContext, R.style.center_dialog_style);
        mDialog.setContentView(view);
        return this;
    }


    public AlertCenterDialog setTitle(String title) {
        mIsShowTitle = true;
        if ("".equals(title)) {
            mTvTitle.setText("标题");
        } else {
            mTvTitle.setText(title);
        }
        return this;
    }

    public AlertCenterDialog setMsg(String msg) {
        mIsShowMsg = true;
        if ("".equals(msg)) {
            mTvMsg.setText("内容");
        } else {
            mTvMsg.setText(msg);
        }
        return this;
    }

    public AlertCenterDialog setCancelable(boolean cancel) {
        mDialog.setCancelable(cancel);
        return this;
    }

    public AlertCenterDialog setPositiveButton(String text,
                                               final View.OnClickListener listener) {
        mIsShowPosBtn = true;
        if ("".equals(text)) {
            mBtnPos.setText("确定");
        } else {
            mBtnPos.setText(text);
        }
        mBtnPos.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                mDialog.dismiss();
            }
        });
        return this;
    }

    public AlertCenterDialog setNegativeButton(String text,
                                               final View.OnClickListener listener) {
        mIsShowNegBtn = true;
        if ("".equals(text)) {
            mBtnNeg.setText("取消");
        } else {
            mBtnNeg.setText(text);
        }
        mBtnNeg.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                listener.onClick(v);
                mDialog.dismiss();
            }
        });
        return this;
    }

    private void setLayout() {
        if (!mIsShowTitle && !mIsShowMsg) {
            mTvTitle.setText("提示");
            mTvTitle.setVisibility(View.VISIBLE);
        }

        if (mIsShowTitle) {
            mTvTitle.setVisibility(View.VISIBLE);
        }

        if (mIsShowMsg) {
            mTvMsg.setVisibility(View.VISIBLE);
        }

        if (!mIsShowPosBtn && !mIsShowNegBtn) {
            mBtnPos.setText("确定");
            mBtnPos.setVisibility(View.VISIBLE);
            mBtnPos.setBackgroundResource(R.drawable.bg_other_room_edit);
            mBtnPos.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    mDialog.dismiss();
                }
            });
        }

        if (mIsShowPosBtn && mIsShowNegBtn) {
            mBtnPos.setVisibility(View.VISIBLE);
            mBtnPos.setBackgroundResource(R.drawable.bg_dialog_right_selector);
            mBtnNeg.setVisibility(View.VISIBLE);
            mBtnNeg.setBackgroundResource(R.drawable.bg_dialog_left_selector);
            mDivider.setVisibility(View.VISIBLE);
        }

        if (mIsShowPosBtn && !mIsShowNegBtn) {
            mBtnPos.setVisibility(View.VISIBLE);
            mBtnPos.setBackgroundResource(R.drawable.bg_dialog_single_selector);
        }

        if (!mIsShowPosBtn && mIsShowNegBtn) {
            mBtnNeg.setVisibility(View.VISIBLE);
            mBtnNeg.setBackgroundResource(R.drawable.bg_dialog_single_selector);
        }
    }

    public void show() {
        setLayout();
        mDialog.show();
    }
}
